package com.comstar.cnp.caserver.controller;

import cn.hutool.core.io.IoUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.comstar.cnp.service.interfaces.caserver.CertManageBusiness;
import com.comstar.cnp.service.interfaces.caserver.entiies.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClientController
 *
 * @author wangrui
 * @date 2022/2/16 15:24
 */
@Slf4j
@RestController
@Api(tags = "证书管理")
@RequestMapping("/ca-server")
public class ClientController {
    @Reference(version = "1.0.0")
    CertManageBusiness certManageBusiness;

    @ApiOperation("查询证书请求列表")
    @GetMapping("list")
    public Object list(QueryCertReqVO queryCertReqBO) {
        BaseResVO<PageListVO<CertReqVO>> certReqList = certManageBusiness.getCertReqList(queryCertReqBO);
        return certReqList;
    }

    @ApiOperation("创建证书请求")
    @PostMapping
    public Object create(CreateCertReqVO createCertReqBO) {
        BaseResVO certReq = certManageBusiness.createCertReq(createCertReqBO);
        return certReq;
    }

    @ApiOperation("导出证书请求")
    @PostMapping("export/{certId}")
    public void export(@PathVariable("certId") Long certId, HttpServletResponse servletResponse) {
        BaseResVO<byte[]> baseRes = certManageBusiness.exportCertReq(certId);
        if (!baseRes.isSuccess()) {
            log.error(baseRes.getMsg());
            return;
        }
        writeOutStream(servletResponse, baseRes.getData());
    }

    private void writeOutStream(HttpServletResponse servletResponse, byte[] data) {
        try {
            ServletOutputStream outputStream = servletResponse.getOutputStream();
            IoUtil.write(outputStream, false, data);
        } catch (IOException e) {
            log.error("写文件失败", e);
        }
    }


    @ApiOperation("导入证书")
    @PostMapping("import/{certId}")
    public Object importCSR(@PathVariable("certId") Long certId, MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            BaseResVO baseRes = certManageBusiness.importCert(certId, bytes);
            return baseRes;
        } catch (IOException e) {
            log.error("读取文件失败", e);
            return BaseResVO.fail(e.getLocalizedMessage());
        }

    }

    @ApiOperation("导出ks")
    @GetMapping(value = "exportKs/{certId}")
    public void exportKs(@PathVariable("certId") Long certId, HttpServletResponse servletResponse) {
        BaseResVO<byte[]> baseRes = certManageBusiness.exportKs(certId);
        if (!baseRes.isSuccess()) {
            log.error("导出失败，{}", baseRes.getMsg());
            return;
        }
        servletResponse.setHeader("Content-disposition", "attachment;filename=client.ks");
        writeOutStream(servletResponse, baseRes.getData());
    }

    @ApiOperation("删除证书请求")
    @DeleteMapping("{certId}")
    public Object delete(@PathVariable("certId") Long certId) {
        BaseResVO baseRes = certManageBusiness.delCertReq(certId);
        return baseRes;
    }


}
